package cn.tpshion.redismq.handler;

import cn.tpshion.redismq.config.RedisMqProperties;
import cn.tpshion.redismq.data.UserMsg;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MessageProducer {

    @Resource
    private RedisTemplate<String, UserMsg> redisTemplate;

    @Resource
    private RedisMqProperties properties;

    public void sendMsg(String msg) {
        ObjectRecord<String, String> record = StreamRecords.newRecord()
                .in(properties.getTopic())
                .ofObject(msg);

        RecordId recordId = redisTemplate.opsForStream().add(record);
        log.info("发送消息: msg:{} recordId:{}", msg, recordId);
    }
}
